import { createApp } from 'vue';
import pinia from '@/store/index';
import App from './App.vue';
import router from './router/index';
import { i18n } from '@/languages/index';
import '@/assets/style.less';
import "@/assets/rem"
import { MessageMapRegister } from '@/netBase/MessageMapRegister';
import { Page } from './store/page';
import { getFastestUrl } from './utils/useStoreMethods';
import H5Websocket from "@/net/Net1";
import { Local } from './utils/storage';


export const getLocale = async (settings: any) => { // Page(pinia).$state.settings
    const adminI18nRes = await fetch(settings.admin_i18n_add_url + '?' + new Date().getTime())
    const adminI18n = await adminI18nRes.json()
    i18n.global.mergeLocaleMessage('zh', adminI18n.zh)
    i18n.global.mergeLocaleMessage('vn', adminI18n.vn)
    i18n.global.mergeLocaleMessage('en', adminI18n.en)
    await Page(pinia).setAdminI18n(adminI18n)
}

export const getSetting = async () => {
    // 53测试环境
    // const settingsRes2 = await fetch('https://yuenan-1317897257.cos.ap-singapore.myqcloud.com/yuenan/config_pk_test.json?' + new Date().getTime());

    const settingsRes1 = await fetch('http://18.167.187.79/config_h5_jason.json' + '?' + new Date().getTime())
    const settings = await settingsRes1.json() // todo试图不要本地settings，项目会报错
    //  使用新封装的ws重连 简化逻辑操作



    const homeGameDataRes = await fetch(settings.third_game_manage + '?' + new Date().getTime())
    const homeGameData = await homeGameDataRes.json()
    await Page(pinia).setHomePageGameData(homeGameData)
    const lang_list_label: any = {
        en: 'home_page_en',
        zh: 'home_page_zh',
        vn: 'home_page_vi',
    }
    settings.lang_list.forEach((item: any) => {
        item.label = lang_list_label[item.name]
        item.value = item.name
    });
    // localStorage.setItem('chat_url', settings.chat_url)
    const chatSettingsRes = await fetch(settings.chat_setting)
    const chatSettings = await chatSettingsRes.json()

    settings.customer_server = chatSettings.data.info_url; //登录聊天室前获取角色地址
    settings.upload_url = chatSettings.data.upload_url; //图片上传、回显地址

    await Page(pinia).setSettings(settings)
    const ipPort = await getFastestUrl()
    await H5Websocket.conectWebsocket(ipPort)
    // console.log(H5Websocket.instance);
    MessageMapRegister.register();
    await getLocale(settings)
    // 获取未读数量
    Page(pinia).getUnread(true)
    setInterval(() => {
        Page(pinia).getUnread(false)
    }, 10000)
    let langObj: any = {
        'en-US': 'en',
        en: 'en',
        'en-AU': 'en',
        zh: 'zh',
        'zh-CN': 'zh',
        vi: 'vi',
        'vi-VN': 'vn',
        vn: 'vn'
    }
    if (Local.get('lang')) {
        i18n.global.locale.value = langObj[Local.get('lang')]
        await Page(pinia).setLang(Local.get('lang'))
    } else {
        let lang = langObj[navigator.language]
        i18n.global.locale.value = lang || 'vn'
        await Page(pinia).setLang(lang || 'vn')
        Local.set('lang', lang || 'vn')
    }
}

const app = createApp(App);
app.use(router).use(pinia).use(i18n);
await getSetting()
app.mount('#app');